package com.puboot.module.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.puboot.common.util.CoreConst;
import com.puboot.common.util.Pagination;
import com.puboot.common.util.ResultUtil;
import com.puboot.module.admin.model.BizBanner;
import com.puboot.module.admin.service.BizBannerService;
import com.puboot.module.admin.vo.base.PageResultVo;
import com.puboot.module.admin.vo.base.ResponseVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Description
 * @Author 申紫微
 * @Data 2020-03-14 21:15:33
 * @Version 1.0
 */
@Controller
@RequestMapping("banner")
@AllArgsConstructor
public class BannerController {

    private final BizBannerService bannerService;

    /**
     * @Author 申紫微
     * @Description Banner列表页面
     * @Date 9:55 2020/3/15
     * @Param [model]
     * @return java.lang.String
     **/
    @GetMapping("/list")
    public String loadBannerListView(Model model){
        return CoreConst.ADMIN_PREFIX + "banner/list";
    }

    /**
     * @Author 申紫微
     * @Description 获取轮播图列表数据
     * @Date 21:27 2020/3/14
     * @Param []
     * @return com.puboot.module.admin.vo.base.ResponseVo
     **/
    @PostMapping("/list")
    @ResponseBody
    public PageResultVo loadBanner(BizBanner banner, Integer pageNumber, Integer pageSize) {
        IPage<BizBanner> page = new Pagination<>(pageNumber, pageSize);
        List<BizBanner> list = bannerService.pageBanner(page,banner);
        return ResultUtil.table(list,page.getTotal());
    }

    /**
     * @Author 申紫微
     * @Description 添加banner的页面
     * @Date 9:58 2020/3/15
     * @Param [model]
     * @return java.lang.String
     **/
    @GetMapping("/add")
    public String addBannerView(Model model){
        return CoreConst.ADMIN_PREFIX + "banner/add";
    }


    /**
     * @Author 申紫微
     * @Description 新增轮播图
     * @Date 21:26 2020/3/14
     * @Param [banner]
     * @return com.puboot.module.admin.vo.base.ResponseVo
     **/
    @PostMapping("/add")
    @ResponseBody
    public ResponseVo addBanner(BizBanner banner) {
        if (bannerService.save(banner)) {
            return ResultUtil.success();
        }
        return ResultUtil.error();
    }

    /**
     * @Author 申紫微
     * @Description 删除轮播图
     * @Date 21:26 2020/3/14
     * @Param [id]
     * @return com.puboot.module.admin.vo.base.ResponseVo
     **/
    @PostMapping("/delete")
    @ResponseBody
    public ResponseVo deleteBanner(Integer id){
        if (Objects.isNull(id)) {
            return ResultUtil.error();
        }
        if(bannerService.deleteBanner(id)){
            return ResultUtil.success();
        }
        return ResultUtil.error();
    }

    /**
     * @Author 申紫微
     * @Description 编辑banner页面
     * @Date 9:58 2020/3/15
     * @Param [model]
     * @return java.lang.String
     **/
    @GetMapping("/edit")
    public String editBannerView(Model model,Integer id){
        BizBanner banner = bannerService.selectById(id);
        model.addAttribute("banner",banner);
        return CoreConst.ADMIN_PREFIX + "banner/detail";
    }

    /**
     * @Author 申紫微
     * @Description 更新轮播图
     * @Date 21:26 2020/3/14
     * @Param [banner]
     * @return com.puboot.module.admin.vo.base.ResponseVo
     **/
    @PostMapping("/edit")
    @ResponseBody
    public ResponseVo updateBanner(BizBanner banner) {
        if(bannerService.updateById(banner)){
            return ResultUtil.success();
        }
        return ResultUtil.error();
    }


    /**
     * @Author 申紫微
     * @Description 批量删除轮播图
     * @Date 21:27 2020/3/14
     * @Param [ids]
     * @return com.puboot.module.admin.vo.base.ResponseVo
     **/
    @PostMapping("/batchDelete")
    @ResponseBody
    public ResponseVo batchDelete(@RequestParam("ids[]") Integer[] ids){
        if (bannerService.batchDelete(ids)){
            return ResultUtil.success();
        }else {
            return ResultUtil.error();
        }
    }

    /**
     * @Author 申紫微
     * @Description 获取详情
     * @Date 21:26 2020/3/14
     * @Param [id]
     * @return com.puboot.module.admin.vo.base.ResponseVo
     **/
    @GetMapping("/getBannerById")
    @ResponseBody
    public ResponseVo getBannerById(Integer id) {
        if (Objects.isNull(id)) {
            return ResultUtil.error("参数异常");
        }
        BizBanner banner = bannerService.getById(id);
        return ResultUtil.success("", banner);
    }

}
